package org.huluobo.controller;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.huluobo.model.User;
import org.huluobo.service.UserService;
import org.huluobo.utils.FormatDateUtils;
import org.huluobo.utils.MD5Util;
import org.huluobo.utils.ResponseMolce;
import org.huluobo.utils.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/userAction")
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 注册发送手机验证码
	 * 
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/registered_SMS")
	public ResponseMolce<String> registered_SMS(HttpServletRequest request, HttpServletResponse response) {
		String phone = request.getParameter("phone");

		User userInfo = userService.getByPhone(phone);
		if (userInfo != null) {
			return new ResponseMolce<String>(0, "ERRO", "手机号已注册");
		}

		String code = SMSUtil.getCode();
		if (SMSUtil.sendSMS(code, phone)) {
			request.getSession().setAttribute("code", code);
			request.getSession().setAttribute("SMSsendTime", new Date());
			return new ResponseMolce<String>(1, "OK", "发送成功");
		} else {
			return new ResponseMolce<String>(0, "ERRO", "发送失败");
		}
	}

	/**
	 * 手机号验证，是否注册过
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/registered_phone")
	public ResponseMolce<String> registered_phone(HttpServletRequest request, HttpServletResponse response) {
		String phone = request.getParameter("phone");
		User userInfo = userService.getByPhone(phone);
		if (userInfo != null) {
			return new ResponseMolce<String>(0, "ERRO", "手机号已注册");
		} else {
			return new ResponseMolce<String>(1, "OK", "可以使用");
		}
	}

	/**
	 * 注册
	 * 
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/registered")
	public ResponseMolce<String> registered(User user, HttpServletRequest request, HttpServletResponse response) {
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		// 获取到发送验证码的时间
		Date SMSsendTime = (Date) request.getSession().getAttribute("SMSsendTime");
		Date now = new Date();
		// 与现在的时间差（以秒为单位）
		long s = (now.getTime() - SMSsendTime.getTime()) / 1000;
		if (s > 60) {
			return new ResponseMolce<String>(0, "ERRO", "验证码失效");
		}

		String code = (String) request.getSession().getAttribute("code");
		if (!user.getCode().equals(code)) {
			return new ResponseMolce<String>(0, "ERRO", "验证码错误");
		}

		user.setId(uuid);
		user.setPassword(MD5Util.encode2hex(user.getPassword()));
		String date = FormatDateUtils.getDate(5);
		user.setCreateDate(date);
		if (userService.add(user)) {
			return new ResponseMolce<String>(1, "OK", "注册成功");
		} else {
			return new ResponseMolce<String>(0, "ERRO", "注册失败");
		}
	}

	/**
	 * 登录（根据手机号）
	 * 
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/login")
	public ResponseMolce<User> login(User user, HttpServletRequest request, HttpServletResponse response) {
		User userInfo = userService.getByPhone(user.getPhone());
		if (MD5Util.validate(user.getPassword(), userInfo.getPassword())) {
			request.getSession().setAttribute("loginUser", userInfo);
			String loginTime = FormatDateUtils.getDate(5);
			userInfo.setLoginTime(loginTime);
			userService.upLoginTime(userInfo);
			return new ResponseMolce<User>(1, "OK", userInfo);
		} else {
			return new ResponseMolce<User>(0, "ERRO", null);
		}
	}

	/**
	 * 从session中获取当前登录用户
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getLoginUser")
	public ResponseMolce<User> getLoginUser(HttpServletRequest request, HttpServletResponse response) {
		User userInfo = (User) request.getSession().getAttribute("loginUser");
		if (userInfo != null) {
			return new ResponseMolce<User>(1, "OK", userInfo);
		} else {
			return new ResponseMolce<User>(0, "ERRO", null);
		}
	}

	@RequestMapping(value = "/redisSave")
	public ResponseMolce<User> redisSave(HttpServletRequest request, HttpServletResponse response) {
		User user=new User();
		user.setName("测试");
		boolean state = userService.redisSave(user);
		if (state) {
			return new ResponseMolce<User>(1, "OK", null);
		} else {
			return new ResponseMolce<User>(0, "ERRO", null);
		}
	}

}
